package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.
        //2.获取action，判断
        String action = req.getParameter("action");
        CommunityDao communityDao=new CommunityDaoImpl();
        if ("selectAll".equals(action)){
            //3.获取请求信息
            String communityName = req.getParameter("communityName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            //封装到map集合中
            Map<String,String> map=new HashMap<>();
            map.put("communityName",communityName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            //连接数据库
            List<Community> list = communityDao.selectAll(map);
            int total = communityDao.selectCount(map);
            //给出响应
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }else if("delete".equals(action)){
            int communityId = Integer.parseInt(req.getParameter("communityId"));
            int num = communityDao.delete(communityId);
            Result<Community> result=null;
            if (num!=0){
                result=Result.success("删除成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                result=Result.error("删除失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }


        }else if ("update".equals(action)){
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String id = req.getParameter("communityId");
            int communityId=0;
            if (id!=null&&!"".equals(id)){
                communityId=Integer.parseInt(id);
            }
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            String communityCity = req.getParameter("communityCity");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityAddress = req.getParameter("communityAddress");
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode=0;
            if (code!=null&&!"".equals(code)){
                communityAreaCode=Integer.parseInt(code);
            }
            String communityCoordinates = req.getParameter("communityCoordinates");
            Community community=new Community(communityId,  communityName,  communityProvince,  communityCity,  communityDistrict,  communityAddress,  communityLatitude,  communityLongitude,  communityAreaCode,  communityCoordinates);
                if (addOrUpdateFlag.equals("1")){
                    int num = communityDao.insert(community);
                    if (num!=0){
                        Result<Community> result=Result.success("新增数据成功");
                        resp.getWriter().write(JSON.toJSONString(result));
                    }else {
                        Result<Community> result=Result.error("新增数据失败");
                        resp.getWriter().write(JSON.toJSONString(result));
                    }
                }
                else if (addOrUpdateFlag.equals("2")){
                    int num = communityDao.update(community);
                    if (num!=0){
                        Result<Community> result=Result.success("更新数据成功");
                        resp.getWriter().write(JSON.toJSONString(result));
                    }else {
                        Result<Community> result=Result.error("更新数据失败");
                        resp.getWriter().write(JSON.toJSONString(result));
                    }
                }
        }
        else if (action.equals("delete")){
            int communityId = Integer.parseInt(req.getParameter("communityId"));
            int num = communityDao.delete(communityId);
            if (num!=0){
                Result<Community> result=Result.success("删除成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result<Community> result = Result.error("删除失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }
        else if (action.equals("associatedStoresList")){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityId=req.getParameter("communityId");
            Map<String,String> map=new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("communityId",communityId);
            List<Shop> list = communityDao.associatedStoresList(map);
            int num = communityDao.countShop(Integer.parseInt(communityId));
            Map resultMap=new HashMap();
            resultMap.put("total",num);
            resultMap.put("list",list);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }
        else if (action.equals("addAssociatedStores")){
            int num=0;
            String ids = req.getParameter("fendeShopCommunityList");
            String[] fendeShopCommunityLists=ids.split(",");
            String panghuCommunityId = req.getParameter("panghuCommunityId");
            for (String id:fendeShopCommunityLists){
                num+=communityDao.addAssociatedStores(Integer.parseInt(id), Integer.parseInt(panghuCommunityId));
            }
            Result result=null;
            if (num!=0){
                result=Result.success("添加"+num+"个小区");
            }else {
                result=Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if (action.equals("findShopListByAdd")){
            List<Shop> list=new ArrayList<>();
            String id = req.getParameter("shopId");
            int shopId=0;
            if (id!=null&&!"".equals(id)){
                shopId=Integer.parseInt(id);
            }
            String shopName = req.getParameter("shopName");
            String shopAddress = req.getParameter("shopAddress");
            String shopSort = req.getParameter("shopSort");
            String shoporder = req.getParameter("shopOrderCount");
            String cId= req.getParameter("communityId");
            String pn = req.getParameter("pageNum");
            int pageNum=0;
            if (pn!=null&&!"".equals(pn)){
                pageNum=Integer.parseInt(pn);
            }
            String pz=req.getParameter("pageSize");
            int pageSize=0;
            if (pz!=null&&!"".equals(pz)){
                pageSize=Integer.parseInt(pz);
            }
            int communityId=0;
            if (cId!=null&&!"".equals(cId)){
                communityId=Integer.parseInt(cId);
            }
            int shopOrderCount=0;
            if (shoporder!=null&&!"".equals(shoporder)){
                shopOrderCount=Integer.parseInt(shoporder);
            }
            Map<String,String> map=new HashMap<>();
            map.put("communityId", String.valueOf(communityId));
            map.put("pageNum", String.valueOf(pageNum));
            map.put("pageSize", String.valueOf(pageSize));
            int num = communityDao.countShops(map);
            Shop shop=new Shop(shopId,shopAddress,shopName,shopOrderCount,shopSort,communityId);
            list = communityDao.findShopListByAdd(shop);
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",num);
            Result result=Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }
        else if (action.equals("deleteShopAssociated"))
        {
            String id = req.getParameter("shopId");
            int shopId = Integer.parseInt(id);
            int i = communityDao.deleteShopAssociated(shopId);
            Result result=Result.success("删除"+i+"个关联门店");
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
